package com.xh.bussiness.exam.controller;

import com.xh.bussiness.exam.bean.dto.ExerciseDto;
import com.xh.bussiness.exam.service.IStudentExerciseService;
import com.xh.bussiness.exam.model.StudentExercise;
import com.xh.bussiness.exam.bean.request.StudentExerciseRequest;
import com.xh.core.bean.XhExportHeaderBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import org.springframework.validation.annotation.Validated;
import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;

import org.springframework.web.bind.annotation.RestController;
import com.xh.core.controller.BaseController;

/**
 * <p>
 * 课堂练习学生记录表 前端控制器
 * </p>
 *
 * @author Pitt Cheung
 * @since 2021-08-26
 */
@RestController
@RequestMapping("/admin/studentExercise")
public class StudentExerciseController extends BaseController {

    @Autowired
    private IStudentExerciseService studentExerciseService;

    /**
     * 分页数据查询
     * @param page 分页对象
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/queryPage")
    public XhPage queryPage(XhPage page, StudentExerciseRequest bean){
        page=studentExerciseService.queryStudentExercisePage(page,bean);
        return page;
    }

    /**
     * 数据查询
     * @param bean 查询条件Bean
     * @return
     */
    @PostMapping("/query")
    public AjaxResult query(StudentExerciseRequest bean){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",studentExerciseService.queryStudentExercise(bean));
    }

    /**
     * 导出全部
     * @param bean
     * @return
     */
    @PostMapping(value="/exportAll")
    public AjaxResult exportAll(StudentExerciseRequest bean, HttpServletRequest request) {
        try {
            LinkedHashMap<String,XhExportHeaderBean> exportHeaderBeans=new LinkedHashMap<>();
            //exportHeaderBeans.put("username",new XhExportHeaderBean("用户名"));
            //exportHeaderBeans.put("deleted",new XhExportHeaderBean("是否删除",ImmutableMap.of(0,"否",1,"是")));
            //exportHeaderBeans.put("createTime",new XhExportHeaderBean("创建时间","yyyy-MM-dd HH:mm:ss"));

            String downloadUrl=studentExerciseService.exportAll(request,exportHeaderBeans,bean,"queryStudentExercisePage");
            if(downloadUrl!=null){
                return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",downloadUrl);
            }else{
                return new AjaxResult(AjaxResultConstant.Result.ERROR,"导出失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(AjaxResultConstant.Result.ERROR);
        }
    }

    /**
     * 新增和保存
     * @param studentExercise 实体对象
     * @return
     */
    @PostMapping(value="/saveOrUpdate")
    @Transactional
    public AjaxResult saveOrUpdate(@Validated StudentExercise studentExercise){
        if(studentExercise.getId()==null){
            studentExerciseService.insert(studentExercise);
        }else{
            studentExerciseService.update(studentExercise);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 点击开始评分按钮，自动生成当前课次的学生信息
     * @author 赵文博
     * @date 2021/9/1 15:56
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/addStudentExercise")
    public AjaxResult addStudentExercise(@RequestBody StudentExerciseRequest bean){
        boolean flag = studentExerciseService.addStudentExercise(bean);
        return flag ? new AjaxResult(AjaxResultConstant.Result.SUCCESS):new AjaxResult(AjaxResultConstant.Result.ERROR);
    }

    /**
     * 学生评价列表
     * @author 赵文博
     * @date 2021/9/1 15:59
     * @param bean
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/studentExerciseList")
    public AjaxResult queryStudentExerciseList(StudentExerciseRequest bean){
        ExerciseDto exerciseDto = studentExerciseService.queryStudentExerciseList(bean);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",exerciseDto);
    }

    /**
     * 给试卷评分
     * @author 赵文博
     * @date 2021/9/1 15:55
     * @param request
     * @return com.xh.core.bean.AjaxResult
     **/
    @Transactional
    @PostMapping("/marking")
    public AjaxResult marking(@RequestBody StudentExerciseRequest request){
        boolean flag = studentExerciseService.marking(request);
        return flag ? new AjaxResult(AjaxResultConstant.Result.SUCCESS):new AjaxResult(AjaxResultConstant.Result.ERROR);
    }



    /**
     * 修改评价
     * @author 赵文博
     * @date 2021/9/1 16:03
     * @param studentExercise
     * @return com.xh.core.bean.AjaxResult
     **/
    @PostMapping("/updateComment")
    public AjaxResult updateComment(StudentExercise studentExercise){
        studentExercise.setStatus(1);
        boolean flag = studentExerciseService.updateComment(studentExercise);
        return flag ? new AjaxResult(AjaxResultConstant.Result.SUCCESS):new AjaxResult(AjaxResultConstant.Result.ERROR);
    }
    /**
     * 删除操作
     * @param id
     * @return
     */
    @PostMapping("/deleteById/{id}")
    @Transactional
    public AjaxResult deleteById(@PathVariable String id){
        studentExerciseService.delete(id);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    /**
     * 批量删除操作
     * @param ids
     * @return
     */
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        studentExerciseService.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }
}

